package com.design.testdemo.activity;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.design.testdemo.R;
import com.design.testdemo.adapter.RecycleViewAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class DownloadingActivity extends AppCompatActivity {

    @BindView(R.id.blelist)
    RecyclerView mBlelist;
    private List<Integer> mList;
    private Handler mHandler = new Handler();
    private RecycleViewAdapter mRecycleViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_downloading);
        ButterKnife.bind(this);

        mRecycleViewAdapter = new RecycleViewAdapter(this);
        mBlelist.setAdapter(mRecycleViewAdapter);
        mBlelist.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        initData();
    }

    private void initData() {

        new Thread(new Runnable() {
            @Override
            public void run() {

                for (int i = 100; i > 0; i--) {
                    //mList.add(i);

                    final int finalI = i;
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            mRecycleViewAdapter.setData(finalI);
                            mBlelist.smoothScrollToPosition(mRecycleViewAdapter.getItemCount());

                        }
                    });
                    SystemClock.sleep(200);
                }

            }
        }).start();
        mList = new ArrayList<>();

    }


    private void downLoading() {

        String sdcard = Environment.getExternalStorageDirectory().toString();

        OkGo.get("http://connect.coollang-overseas.com/version/down?token=k2WZCdbXoPwOJKZiBTPJOnZVlJ1f9_JhaoubxVP6h3AuoNrmwLVzXUBU7ozLoQAi")
                .tag(this)
                .execute(new FileCallback(sdcard, "abc") {  //文件下载时，可以指定下载的文件目录和文件名
                    @Override
                    public void onSuccess(File file, Call call, Response response) {
                        // file 即为文件数据，文件保存在指定目录
                        Log.i("下载成功", "progress: " + file.getPath());
                    }

                    @Override
                    public void downloadProgress(long currentSize, long totalSize, float progress, long networkSpeed) {
                        //这里回调下载进度(该回调在主线程,可以直接更新ui)
                        Log.i("下载进度", "progress: " + progress);
                    }
                });

    }
}
